package com.huanglj.himalaya.utils;

import com.huanglj.himalaya.base.BaseFragment;
import com.huanglj.himalaya.fragments.HistoryFragment;
import com.huanglj.himalaya.fragments.RecommendFragment;
import com.huanglj.himalaya.fragments.SubscriptionFragment;

import java.util.HashMap;
import java.util.Map;

import static com.huanglj.himalaya.utils.Constants.INDEX_HISTORY;
import static com.huanglj.himalaya.utils.Constants.INDEX_RECOMMEND;
import static com.huanglj.himalaya.utils.Constants.INDEX_SUBSCRIPTION;

public class FragmentCreator {


    private static Map<Integer, BaseFragment> sCache = new HashMap<>();

    public static BaseFragment getFragment(int index){
        //如果有缓存就加载
        BaseFragment baseFragment = sCache.get(index);
        if (baseFragment != null) {
            return baseFragment;
        }
        switch (index){
            case INDEX_RECOMMEND:
                baseFragment = new RecommendFragment();
                break;
            case INDEX_SUBSCRIPTION:
                baseFragment = new SubscriptionFragment();
                break;
            case INDEX_HISTORY:
                baseFragment = new HistoryFragment();
                break;
        }
        sCache.put(index,baseFragment);
        return baseFragment;
    }
}
